package com.base.monitor.module.biz.service.cfg.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.base.monitor.framework.result.PageResult;
import com.base.monitor.framework.util.CustomerUtils;
import com.base.monitor.model.model.biz.cfg.crierita.IndicatorCriteria;
import com.base.monitor.model.model.biz.cfg.entity.Indicator;
import com.base.monitor.model.model.biz.cfg.vo.IndicatorVo;
import com.base.monitor.module.biz.context.CfgContext;
import com.base.monitor.module.biz.service.cfg.IndicatorService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndicatorServiceImpl implements IndicatorService {
    /**
     * 加载指标详情模态框
     *
     * @param id 指标id
     * @return
     */
    @Override
    public IndicatorVo findOne(String id) {
        List<Indicator> indicatorAllVoList = CfgContext.getIndicatorAllVoList();
        if (CollectionUtil.isEmpty(indicatorAllVoList)) {
            return null;
        }
        List<Indicator> collect = indicatorAllVoList.stream()
                .filter(item -> item.getIndicatorId().equals(id))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(collect)) {
            return null;
        }
        return new IndicatorVo(collect.get(0));
    }

    /**
     * 分页条件查询指标信息
     *
     * @param criteria 查询条件
     * @return
     */
    @Override
    public PageResult<Indicator> page(IndicatorCriteria criteria) {
        List<Indicator> indicatorAllVoList = CfgContext.getIndicatorAllVoList();
        if (StrUtil.isNotBlank(criteria.getGatewayIp())) {
            indicatorAllVoList = indicatorAllVoList.stream()
                    .filter(item -> item.getGatewayIp().contains(criteria.getGatewayIp()))
                    .collect(Collectors.toList());
        }

        int total = indicatorAllVoList.size();
        List<Indicator> content = CustomerUtils
                .getSubListForPage(indicatorAllVoList, criteria.getPage(), criteria.getSize());
        fillOnlineTimeAndStatus(content);
        return new PageResult<>(total, content);
    }

    /**
     * 填充返回结果的在线时间和状态
     * @param content 返回结果
     */
    private void fillOnlineTimeAndStatus(List<Indicator> content) {
        if (CollectionUtil.isEmpty(content)) {
            return;
        }
        for (Indicator vo : content) {
            Date onlineTime = CfgContext.getIndicatorOnlineMap().get(vo.getIndicatorId());
            if (onlineTime == null
                    || System.currentTimeMillis() - onlineTime.getTime() >= getTimeout(vo.getFrequency())) {
                vo.setStatus(0);
            } else {
                vo.setStatus(1);
            }
            vo.setOnlineTime(onlineTime);
        }
    }

    /**
     * 获取指标的离线时间，应该为采集频率的三倍（单位毫秒）
     * @param frequency 采集频率
     * @return
     */
    private long getTimeout(Integer frequency) {
        if (frequency == null) {
            return 60 * 1000;
        }
        switch (frequency) {
            case 1:
                return 60 * 1000 * 3;
            case 2:
                return 10 * 60 * 1000 * 3;
            case 3:
                return 30 * 60 * 1000 * 3;
            case 4:
                return 60 * 60 * 1000 * 3;
            case 5:
                return 12 * 60 * 60 * 1000 * 3;
            case 6:
                return 500 * 3;
            default:
                return 60 * 1000;
        }
    }
}
